<template>
    <div>
      <van-search
        v-model="keywords"
        show-action
        label="搜索商品"
        placeholder="请输入搜索关键词"
        @search="onSearch"
        >
        <template #action>
            <div @click="onSearch">搜索</div>
        </template>
        </van-search>
        <div class="seek">
            <div class="kong" v-if="!his.length">你还没有收索过任何商品</div>
            <div class="content" @click="clickHis(i)" v-for="i in his" :key="i">{{i}}</div>
        </div>
    </div>
</template>

<script>
export default {
  data () {
    return {
      keywords: '',
      his: localStorage.getItem('his')
        ? JSON.parse(localStorage.getItem('his'))
        : []
    }
  },
  methods: {
    clickHis (his) {
      this.keywords = his
      this.onSearch()
    },
    onSearch () {
      // 拿和关键字
      let his = localStorage.getItem('his')
        ? JSON.parse(localStorage.getItem('his'))
        : []
      his.unshift(this.keywords)
      // 存之前 做一个数组去重处理
      his = [...new Set(his)]
      // 控制长度 历史记录最多5个
      his.length = 8
      // 存回去
      localStorage.setItem('his', JSON.stringify(his))
      this.$router.push({
        path: '/searchResult',
        query: {
          word: this.keywords
        }
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.seek{
    display: flex;
    padding: 4px 5px;
    flex-wrap: wrap;
    .content{
        width: 40xp;
        height: 25px;
        margin-right: 8px;
        overflow: hidden;
        text-overflow:ellipsis;
         white-space: nowrap;
         background-color: #fafafa;
         line-height: 25px;
    }
    .kong{
        color:rgba(230, 124, 26, 0.4)
    }
}
</style>
